package com.guigusuqi.commonutils.vo;

import com.guigusuqi.commonutils.constant.ResultCodeConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("返回给前端的结果")
public class Result<T> {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    //  不允许通过new的方式创建Result对象
    public Result() {
    }

    public Result(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public Result(Boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //  public和ResponseResult间的<T>指定的这是一个泛型方法，这样才可以在方法内使用T类型的变量
    public static <T> Result<T> success() {
        return new Result<>(true, ResultCodeConstant.SUCCESS.getCode(), ResultCodeConstant.SUCCESS.getMsg());
    }

    //  public和ResponseResult间的<T>指定的这是一个泛型方法，这样才可以在方法内使用T类型的变量
    public static <T> Result<T> success(String message) {
        return new Result<>(true, ResultCodeConstant.SUCCESS.getCode(), message);
    }

    //  public和ResponseResult间的<T>指定的这是一个泛型方法，这样才可以在方法内使用T类型的变量
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(true, ResultCodeConstant.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(true, ResultCodeConstant.SUCCESS.getCode(), ResultCodeConstant.SUCCESS.getMsg(), data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(false, ResultCodeConstant.FAILED.getCode(), ResultCodeConstant.FAILED.getMsg(), null);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(false, ResultCodeConstant.FAILED.getCode(), msg);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return new Result<>(false, ResultCodeConstant.FAILED.getCode(), msg, data);
    }

    public static <T> Result<T> fail(ResultCodeConstant errorCode) {
        return new Result<>(false, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> Result<T> fail(ResultCodeConstant errorCode, T data) {
        return new Result<>(false, errorCode.getCode(), errorCode.getMsg(), data);
    }

    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
}
